﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class Turret : MonoBehaviour
{
    public List<GameObject> enemyList;  //警戒范围敌人列表引用
    private TurrotHR baseHRObjScript;   //水平旋转的物体脚本
    public FireControlsystem fireControlSystemScript = null;

    private void Awake()
    {
        if(baseHRObjScript==null)
        {
            baseHRObjScript = transform.Find("TurretA_Base_C_low").GetComponent<TurrotHR>();
        }
        enemyList = new List<GameObject>();
        if(fireControlSystemScript==null)
        {
            fireControlSystemScript = gameObject.GetComponent<FireControlsystem>();
        }
    }

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        //enemyList.Remove(null);

    }

    //固定时间刷新，默认是0.2秒刷新一次
    private void FixedUpdate()
    {

        //有敌人
        if (enemyList.Count > 0)
        {
            if (baseHRObjScript != null)
            {
                baseHRObjScript.isRun = true;
            }
            if(fireControlSystemScript!=null)
            {
                fireControlSystemScript.isRun = true;
            }

        }
        else
        {
            if (baseHRObjScript != null)
            {
                baseHRObjScript.isRun = false;
            }
            if (fireControlSystemScript != null)
            {
                fireControlSystemScript.isRun = false;
            }
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if(other.tag.Equals("Enemy"))
        {
            enemyList.Add(other.gameObject);
        }
    }

    private void OnTriggerExit(Collider other)
    {
        if(other.tag.Equals("Enemy"))
        {
            enemyList.Remove(other.gameObject);
        }
    }

    public void checkListNullElement()
    {
        ClearListNullElement();
    }

    IEnumerator ClearListNullElement()
    {
        yield return new WaitForSeconds(0.1f);
        enemyList.Remove(null);
    }
}
